<?php
class GuestsController extends AppController {
    public $name    = 'Guests';
    public $helpers = array('Form');
    public $scaffold;

    public function rest_list ($ext='xml') {
        $data = $this->Guest->find('all');
        $this->rest_render($ext, $data);
    } // rest_list

    public function rest_login ($ext='xml') {
        if (!$params = $this->rest_input($ext)) {
            return;
        }

        $data = $this->Guest->find('first', array(
            'conditions' => array(
                'Guest.email'    => $params['email'],
                'Guest.password' => $params['password']
            ),
            'recursive'  => -1
        ));

        if (!$data) {
            $resp['response'] = array(
                'status'  => -1,
                'message' => __('Invalid email or password', true)
            );

            $this->rest_render($ext, $resp);
            return;
        } else if ($data['Guest']['status'] != 'A') {
            $resp['response'] = array(
                'status'  => -1,
                'message' => __('Account inactive, please contact administrator', true)
            );

            $this->rest_render($ext, $resp);
            return;
        }

        $resp = array(
            'response' => array(
                'status'  => 1,
                'message' => __('Login successfully.', true),
                'guest'   => $data['Guest']
            )
        );

        $this->rest_render($ext, $resp);
    } // rest_login


    public function listing ($type=null) {
        $json = $this->Guest->guestList($this->params['form']);
        $this->response($json);
    } // listing

    public function save () {
        $this->data['Guest'] = $this->params['form'];

        if (!$this->data['Guest'] || count($this->data['Guest']) == 0) { // check if data post
            $json = array(
                'status' => false,
                'mesg'   => __('no data posted', true)
            );

            $this->response($json);
            return;
        }

        foreach ($this->data['Guest'] as $fieldname=>$value) { // convert empty data to null
            if (empty($value) && $value != '0') {
                $this->data['Guest'][$fieldname] = null;
            }
        }

        $this->Guest->set($this->data);

        if (!$this->Guest->validates()) { // invalid data found!
            $json = array(
                'status' => false,
                'mesg'   => __('Please enter the details in the fields pointed out above and try again!', true)
            );

            foreach ($this->Guest->invalidFields() as $k=>$v) {
                $json['error'][] = array(
                    'f' => $k,
                    'm' => $v
                );
            }

            $this->response($json);
            return;
        }

        if (!$this->Guest->save($this->data)) { // error when saving data
            $mesg = empty($this->params['form']['id']) ? __('Fail insert record to database at this time, please try again.', true) : __('Fail update record to database at this time, please try again.', true);
            $err  = ($this->error) ? $this->error : $mesg;
            $json = array(
                'status' => false,
                'mesg'   => $err
            );

            $this->response($json);
            return;
        }

        $mesg = empty($this->params['form']['id']) ? __('New profile has been created successfully!',true) : __('Profile has been updated successfully!',true);
        $json = array(
            'status' => true,
            'mesg'   => $mesg
        );

        $this->response($json);
    } // save

    public function update () {
        $form = $this->params['form'];

        $this->Guest->updateAll(
            array('Guest.status' => '"D"'),
            array('Guest.id' => explode(',', $form['id']))
        );

        if ($num = $this->Guest->getAffectedRows()) {
            $json = array(
                'status'   => true,
                'affected' => $num);
        } else {
            $json = array('mesg' => __('Fail to update record at this time.', true));
        }

        $this->response($json);
    } // update
} // GuestsController
?>